package com.wei.stavix.ui.widget

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.wei.stavix.R
import com.wei.stavix.ui.theme.ColorGray
import com.wei.stavix.ui.theme.ColorPhoneBg
import com.wei.stavix.ui.theme.ColorPrimary

@Composable
fun PhoneView(areaCode: String = "中国大陆 (+86)", modifier: Modifier = Modifier
    .background(ColorPhoneBg, RoundedCornerShape(50))
    .padding(start = 13.dp, top = 6.dp, end = 13.dp, bottom = 6.dp)) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = modifier
    ) {
        Text(
            text = areaCode,
            fontSize = 14.sp,
            color = ColorPrimary,
        )
        Spacer(modifier = Modifier.width(2.dp))
        Image(
            painter = painterResource(id = R.drawable.icon_triangle),
            contentDescription = null
        )
    }
}

@Composable
fun LoginInput(type: Int, modifier: Modifier = Modifier.padding(0.dp).height(45.dp)) {
    Surface(
        color = Color.Transparent,
        modifier = modifier
    ) {
        val hintTxt = when (type){
            1 -> "请输入帐号"
            2 -> "请输入密码"
            3 -> "验证码"
            4 -> "请输入email"
            5 -> "请输入手机号"
            else -> ""
        }
        val startIcon = when(type) {
            1 -> R.drawable.icon_user
            2 -> R.drawable.icon_password
            3 -> R.drawable.icon_security
            4 -> R.drawable.icon_email
            else -> 0
        }
        val endIcon = when(type) {
            1, 4, 5 -> R.drawable.icon_input_delete
            2 -> R.drawable.login_icon_invisible
            else -> 0
        }
        var foucsed by remember { mutableStateOf(false) }
        val keyboardType = if (type == 1) KeyboardOptions.Default else KeyboardOptions(keyboardType = KeyboardType.Password)
        var textValue by remember { mutableStateOf(TextFieldValue("")) }

        Box(modifier = Modifier.background(color = Color.Transparent).fillMaxHeight().padding(bottom = 10.dp)) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier
                    .fillMaxHeight()
                    .background(Color.Transparent)
            ) {
                if (startIcon != 0) {
                    Image(
                        painter = painterResource(id = startIcon),
                        contentScale = ContentScale.Fit,
                        contentDescription = null,
                        modifier = Modifier.width(16.dp).height(16.dp).background(color = Color.Transparent)
                    )
                } else {
                    // input phone
                    PhoneView("+86", modifier = Modifier.padding(top = 6.dp, bottom = 6.dp))
                }

                Spacer(modifier = Modifier.width(10.dp))

                BasicTextField(
                    value = textValue,
                    singleLine = true,
                    cursorBrush = SolidColor(ColorPrimary),
                    modifier = Modifier
                        .weight(1f)
                        .background(color = Color.Transparent)
                        .onFocusChanged {
                            foucsed = it.isFocused
                        },
                    keyboardOptions = keyboardType,
                    textStyle = TextStyle(
                        color = ColorPrimary,
                        textAlign = TextAlign.Start,
                        fontSize = 16.sp,
//                textDecoration = TextDecoration.Underline
                    ),
                    onValueChange = {
                        textValue = it
                    },
                    decorationBox = { innerTextField ->
                        Box(
                            contentAlignment = Alignment.CenterStart,
                            modifier = Modifier
                                .weight(1f),
                        ) {
                            if (textValue.text.isEmpty()) {
                                Text(
                                    text = hintTxt,
                                    color = ColorGray,
                                )
                            }
                            innerTextField()
                        }
                    },
                )
                if (endIcon != 0) {
                    Spacer(modifier = Modifier.width(10.dp))
                    Image(
                        painter = painterResource(id = endIcon),
                        contentScale = ContentScale.Fit,
                        contentDescription = null,
                        modifier = Modifier.width(20.dp).height(20.dp).background(color = Color.Transparent)
                    )
                }
            }

            val lineColor = if (foucsed) ColorPrimary else ColorGray
            Spacer(modifier = Modifier.fillMaxWidth().height(1.dp).background(lineColor).align(alignment = Alignment.BottomCenter))
        }

    }
}